import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import {config} from '../../shared/smartadmin.config';

declare var $: any;
@Component({
	selector: 'sa-flowCount',
	templateUrl: './flowCount.component.html',
})
export class FlowCountComponent implements OnInit {
	countType:string = '0'; //搜索keyword
	tableData:any[] = []; //table数据
	resultTableData:any[] = [] //客户端分页数据暂存
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	searchKey:string = ''; //搜索keyword
	ipNameList:any[] = []; //api数据集合
	startTime:string = '';//开始时间
	endTime:string = '';	//结束时间
	selectedIp:string = '';	//选择的ip
	
	constructor(private jsonApiService: JsonApiService) {}
	
	ngOnInit() {
		this.initDate().then(()=>this.load());//初始化日期,加载table表格数据
		System.import('script-loader!select2/dist/js/select2.min.js').then(()=>{
			System.import('assets/js/select2-zh-CN.js').then(()=>{
				this.queryIpName().then(()=>{this.selectIpName();});
			})
		})
	}
	selectChangeEvent(){
		
		this.initDate();
	}
	//初始化日期   时分秒格式
	initDate(){
		var that = this;
		return new Promise(function (resolve, reject) {
			System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(()=>{
					System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(()=>{
						$('#datetimepicker1').datetimepicker('remove');
						$('#datetimepicker2').datetimepicker('remove');
						if(that.countType=='0'){
							$("#datetimepicker1").datetimepicker({//时间插件
								format: "yyyy-mm-dd hh:ii:ss",
							    language: "zh-CN",
							    autoclose: true,
							}).on("changeDate", function(e){
								var date = e.date;
								if(!date){return;}
								var time = common.getTimeMmss(date);
								that.startTime=time;
								$("#datetimepicker2").datetimepicker("setStartDate", time);
							});
							$("#datetimepicker2").datetimepicker({//时间插件
								format: "yyyy-mm-dd hh:ii:ss",
							    language: "zh-CN",
							    autoclose: true,
							}).on("changeDate", function(e){
								var date = e.date;
								if(!date){return;}
								var time = common.getTimeMmss(date);
								that.endTime=time;
								$("#datetimepicker1").datetimepicker("setEndDate", time);
							});
							var date = new Date();
							var time2=common.getTimeMmss(date)//当前时间
							$("#datetimepicker2").val(time2);
							that.endTime=time2;
							date.setDate(date.getDate() - 7);//设置前一周
							var time1=common.getTimeMmss(date)
							$("#datetimepicker1").val(time1);
							that.startTime=time1;
						}
						else{
							$("#datetimepicker1").datetimepicker({//时间插件
								format: "yyyy-mm-dd",
							    language: "zh-CN",
							    autoclose: true,
						     	minView : 2	
							}).on("changeDate", function(e){
								var date = e.date;
								if(!date){return;}
								var time = common.getTimeDay(date);
								that.startTime=time;
								$("#datetimepicker2").datetimepicker("setStartDate", time);
							});
							$("#datetimepicker2").datetimepicker({//时间插件
								format: "yyyy-mm-dd",
							    language: "zh-CN",
							    autoclose: true,
							    minView : 2	
							}).on("changeDate", function(e){
								var date = e.date;
								if(!date){return;}
								var time = common.getTimeDay(date);
								that.endTime=time;
								$("#datetimepicker1").datetimepicker("setEndDate", time);
							});
							var date = new Date();
							var time2=common.getTimeDay(date)//当前时间
							$("#datetimepicker2").val(time2);
							that.endTime=time2;
							date.setDate(date.getDate() - 7);//设置前一周
							var time1=common.getTimeDay(date)
							$("#datetimepicker1").val(time1);
							that.startTime=time1;
						}
						resolve();
					});
				}
			);
		})
	}
	//擦除api所选值
	delIpName(){	
		//采用简单粗暴的方法清除;官方推荐的方法失效
		this.selectedIp='';
		var str=$("<span class='select2-selection__placeholder'>请选择</span>");
		$('#ipNameSelect2').val("");
		$("#select2-ipNameSelect2-container").empty();
		$("#select2-ipNameSelect2-container").attr("title","");
		$("#select2-ipNameSelect2-container").prepend(str);
	}
	//拉取ip数据
	queryIpName(){
		var that = this;
		return new Promise(function (resolve, reject) {
			that.jsonApiService.http("trafficFlow/getIpList.html", null).subscribe(data => {
				if(data.success){
					var iplist = data.data;
		        	var selectdata = [];
		        	for(var i = 0 ; i < iplist.length ; i++) {
		        		var obj = {
		        			id:i,
		        			text:iplist[i],
		        		}
		        		selectdata[i] = obj;
		        	}
		        	that.ipNameList = selectdata;
		        	resolve();
				}else{
					reject();
				}
			})
		})
	}
	//初始化ip Select2选项
	selectIpName(){
		var that = this;
		$("#ipNameSelect2").select2({
	    	language : "zh-CN",
	    	placeholder :"请选择",
	    	allowClear : true,
	    	data : this.ipNameList,//为Select添加数据
	        templateResult: this.formatRepo2
	    }).on ("select2:select", function (e){
        	  	that.selectedIp=e.params.data.text;
       });
	}



	//select2 格式回调函数
	formatRepo2 (state) {
		if (!state.id) {return state.text};
		var $state = $('<span><i class="fa fa-paperclip green"></i>&nbsp;&nbsp;' + state.text + '</span>');
		return $state;
	}
	//查询
	search_data(){
		this.curPage = 1;
		this.load();
	}
	//模糊搜索
	table_search(){
		this.search_data();
	}
	//刷新
	refresh(){
		this.load();
	}
	//加载list数据
	load() {
		this.startTime =  $('#datetimepicker1').val();
		this.endTime =  $('#datetimepicker2').val();
		if(this.startTime==''||this.endTime==''){
			this.jsonApiService.message("起始结束时间不能为空");
			return;
		}
		let timeCheck=common.correctTimeParams(this.startTime,this.endTime,0);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
	
		var data = {
			parameters:{
				startTime:this.startTime,
				endTime:this.endTime,
				ip:this.selectedIp,
				state:Number(this.countType)
			}
		}
		let dataurl='trafficFlow/atrafficFlowList.html';
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http(dataurl, reqdata).subscribe(data => {
			this.tableData = data.data;
			this.totalCount = data.data.length;
//			if(this.totalCount){
				this.client_pages(0,this.pageSize);
//			}
		})
	}
	client_pages(start,length){
		this.resultTableData = this.tableData.slice(start,start+length);
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.client_pages(e.first,e.rows);
	}
	//删除所选时间
	del_date(){
		this.startTime = '';
		this.endTime ='';
		$("#datetimepicker1").val('').change();
		$("#datetimepicker2").val('').change();
		$("#datetimepicker2").datetimepicker("setStartDate", null);
		$("#datetimepicker1").datetimepicker("setEndDate", null);
	}
}